package es.tid.www.wsdl.parlayx.third_party_call.v1_1.consumer;

import java.rmi.RemoteException;

import org.apache.axis2.databinding.types.URI;

import es.tid.www.wsdl.parlayx.third_party_call.v1_1.PolicyException;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ServiceException;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.CallParticipantInformation;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.CallSession;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.ChargingInformation;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.EndCallSession;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.EndCallSession11;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallParticipantInformation;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallParticipantInformation14;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallParticipantInformationResponse;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallParticipantInformationResponse16;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallSessionInformation;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallSessionInformation17;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallSessionInformationResponse;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.GetCallSessionInformationResponse18;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.MakeCallSession;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.MakeCallSession12;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.MakeCallSessionResponse;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.MakeCallSessionResponse10;
import es.tid.www.wsdl.parlayx.third_party_call.v1_1.ThirdPartyCallServiceStub.MediaInfo;

/**
 * Wrappes the way to call third party call web service. This class lets access third party call parlayx interface
 * easier than doing it directly by objects created with WSDL2Java tool.
 *
 */
public class ThirdPartyCallProxy {

	/**
	 * End point reference where is the server part of this web service. 
	 */
	private String targetEndPoint;
	
	/**
	 * Creates an instance of the proxy to be used in the application wanted to make the ws service interface call
	 * @throws Exception when the configuration properties needed for a success work cannot be loaded.
	 */
	public ThirdPartyCallProxy() throws Exception{
		//read 'parlayx.tpcc.ws.endpoint' config.properties value, to determinate the remote end point of the
		//server part
		targetEndPoint=ThirdPartyCallConfigInfo.getInstance().getConfigProperty("parlayx.tpcc.ws.endpoint");
	}
	
	/**
	 * Interface method to make a direct call. This method invokes the remote webservice to connect to the ims net
	 * @param callParticipants array of uris with the sip addresses of the participants in the call. These addresses are included in Request-URI and To header when the call is made
	 * @param callerIdentities indentities array of uris to be included in From and P-Asserted-Identity header when the call is made
	 * @param calledIdentitiesNames Display names to be included in To header for every session leg
	 * @param callerIdentitiesNames Display names to be included in From and P-Asserted-Identity headers for every session leg
	 * @param charging object with information about charging
	 * @param mediaInfo Identifies one or more media types to be usend during the session. If it's empty, the media is negotiated during session establishmet by the ims participants.
	 * @param changeMediaNotAllowed if true, participants in the session cannot change the media during the call
	 * @return identifier for the session. This identifier lets execute other operations over the same call
	 * @throws PolicyException when some of the gateway service policies is violated when making this call.
	 * @throws ServiceException when the gateway service fails for some reason.
	 */
	 public String makeCallSession(
			 URI[] callParticipants, 
			 URI[] callerIdentities, 
			 String[] calledIdentitiesNames, 
			 String[] callerIdentitiesNames, 
			 ChargingInformation charging, 
			 MediaInfo[] mediaInfo, 
			 boolean changeMediaNotAllowed) throws PolicyException,ServiceException{
		 
		 try{
			 //create an empty object to be the parameter for webservice call
			 MakeCallSession makeCallSession=new MakeCallSession();
			 //fill mandatory callParticipants information
			 makeCallSession.setCallParticipants(callParticipants);
			 //fill callerIdentities if not null
			 if (callerIdentities!=null){
				 makeCallSession.setCallerIdentities(callerIdentities);
			 }
			 //fill calledIdentitiesNames if they aren't null
			 if (calledIdentitiesNames!=null){
				 makeCallSession.setCalledIdentitiesNames(calledIdentitiesNames);
			 }
			 //fill callerIdentitiesNames if they aren't null
			 if (callerIdentitiesNames!=null){
				 makeCallSession.setCallerIdentitiesNames(callerIdentitiesNames);
			 }
			 //fill charging information if it's not null
			 if (charging!=null){
				 makeCallSession.setCharging(charging);
			 }
			 //fill media type to be used in the call if not null
			 if (mediaInfo!=null){
				 makeCallSession.setMediaInfo(mediaInfo);
			 }
			 //set the flag for allowing changing media during call or not
			 makeCallSession.setChangeMediaNotAllowed(changeMediaNotAllowed);
			 
			 //Create an stub object with the EPR readed from config properties file and fill with the received information
			 ThirdPartyCallServiceStub stub=new ThirdPartyCallServiceStub(targetEndPoint);
			 MakeCallSession12 makeCallSession12=new MakeCallSession12();
			 makeCallSession12.setMakeCallSession(makeCallSession);
			 
			 //do the call, and recover the webservice response
			 MakeCallSessionResponse10 response=stub.makeCallSession(makeCallSession12);
			 
			 //unwrap the response and recover the call session Id to be returned by this method.
			 MakeCallSessionResponse makeCallSessionResponse=response.getMakeCallSessionResponse();
			 String callSessionId=makeCallSessionResponse.getResult();
			 return callSessionId;
		 }
		 catch (RuntimeException re){
			 throw ExceptionBuilder.buildPolicyException(re);
		 }
		 catch (RemoteException ex){
			 throw ExceptionBuilder.buildServiceException(ex);
		 }
	 }
	 
	 /**
	  * Invokes the webservice to ask for participant information in current call
	  * @param callSessionIdentifier identifier for session of the call to query
	  * @param callParticipant sip address of a participant in the current call
	  * @return object with the information of the participan in the current call
	  * @throws PolicyException when some of the gateway service policies is violated when making this call
	  * @throws ServiceException when the gateway service fails for some reason.
	  */
	 public CallParticipantInformation getCallParticipantInformation(
			 String callSessionIdentifier, 
			 URI callParticipant) 
	 throws PolicyException, ServiceException{
		 
		 try{
			 //create the stub with the end point readed from config properties
			 ThirdPartyCallServiceStub stub=new ThirdPartyCallServiceStub(targetEndPoint);
			 //fill the information of the object to send to webservice
			 GetCallParticipantInformation getCallParticipantInformation=new GetCallParticipantInformation();
			 getCallParticipantInformation.setCallParticipant(callParticipant);
			 getCallParticipantInformation.setCallSessionIdentifier(callSessionIdentifier);
			 
			 GetCallParticipantInformation14 getCallParticipantInformation14=new GetCallParticipantInformation14();
			 getCallParticipantInformation14.setGetCallParticipantInformation(getCallParticipantInformation);
			 //invoke webservice and get the response.
			 GetCallParticipantInformationResponse16 response=
				 stub.getCallParticipantInformation(getCallParticipantInformation14);
			 
			 //unwrap response to return the result in a easy to work javabean
			 GetCallParticipantInformationResponse getCallParticipantInformationResponse=
				 response.getGetCallParticipantInformationResponse();
			 return getCallParticipantInformationResponse.getResult();
		 }
		 catch (RuntimeException re){
			 throw ExceptionBuilder.buildPolicyException(re);
		 }
		 catch (RemoteException ex){
			 throw ExceptionBuilder.buildServiceException(ex);
		 }
	 }
	 
	 /**
	  * Gets information about the current call
	  * @param callSessionIdentifier identifier for the call to retrieve information about
	  * @return object with the information recovered from the call session.
	  * @throws PolicyException when some of the gateway service policies is violated when making this call
	  * @throws ServiceException when the gateway service fails for some reason.
	  */
	 public CallSession getCallSessionInformation(String callSessionIdentifier)
	 	throws PolicyException, ServiceException{
	 
		 try{
			 //create the stub with the EPR readed from config.properties
			 ThirdPartyCallServiceStub stub=new ThirdPartyCallServiceStub(targetEndPoint);
			 
			 //create an object to be filled with the information received in the method as parameter
			 GetCallSessionInformation getCallSessionInformation=new GetCallSessionInformation();
			 getCallSessionInformation.setCallSessionIdentifier(callSessionIdentifier);
			 GetCallSessionInformation17 getCallSessionInformation17=new GetCallSessionInformation17();
			 getCallSessionInformation17.setGetCallSessionInformation(getCallSessionInformation);
			 //invoke the webservice and get the response
			 GetCallSessionInformationResponse18 response=stub.getCallSessionInformation(getCallSessionInformation17);
			 
			 //unwrap the response to be returned as a javabean
			 GetCallSessionInformationResponse getCallSessionInformationResponse=
				 response.getGetCallSessionInformationResponse();
			 return getCallSessionInformationResponse.getResult();
		 }
		 catch (RuntimeException re){
			 throw ExceptionBuilder.buildPolicyException(re);
		 }
		 catch (RemoteException ex){
			 throw ExceptionBuilder.buildServiceException(ex);
		 }
	 }

	 /**
	  * Finalizes the current call
	  * @param callSessionIdentifier identifier for the call to end
	  * @throws PolicyException when some of the gateway service policies is violated when making this call
	  * @throws ServiceException when the gateway service fails for some reason.
	  */
	 public void endCallSession(String callSessionIdentifier) 
	 	throws PolicyException, ServiceException{
		 
		 try{
			 //create the stub with the EPR readed from the config.properties file
			 ThirdPartyCallServiceStub stub=new ThirdPartyCallServiceStub(targetEndPoint);
			 
			 //create an object to invoke the webservice and fill with the call session identifier
			 EndCallSession endCallSession=new EndCallSession();
			 endCallSession.setCallSessionIdentifier(callSessionIdentifier);
			 EndCallSession11 endCallSession11=new EndCallSession11();
			 endCallSession11.setEndCallSession(endCallSession);
			 //invoke the webservice
			 stub.endCallSession(endCallSession11);
		 }
		 catch (RuntimeException re){
			 throw ExceptionBuilder.buildPolicyException(re);
		 }
		 catch (RemoteException ex){
			 throw ExceptionBuilder.buildServiceException(ex);
		 }
	 }
}
